package com.sangfor.ssl.service.https;

import android.os.Looper;
import android.os.NetworkOnMainThreadException;
import com.sangfor.bugreport.logger.Log;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;

/* loaded from: classes2.dex */
public final class MultiHttpRequest implements Closeable {
    private static final int NATIVE_MULTI_HTTP_REQUEST = 1;
    private static final int NATIVE_WEAK_REF = 2;
    private static final String TAG = "MultiHttpRequest";
    private static final boolean TRACE_CLOSE = true;
    private final long mNativePtr;
    private ResultCallback mResultCallback;
    private boolean mClosed = false;
    private Throwable mStackTrace = new Throwable();
    private final long mNativeWeakSelfPtr = createWeakGlobalRefNative(this);

    /* loaded from: classes2.dex */
    public interface ResultCallback {
        int onResultCallback(String str, int i, HttpResponse httpResponse);
    }

    private static native long createRequestsNative(String[] strArr, String[] strArr2);

    private static native long createWeakGlobalRefNative(Object obj);

    private static native void destroyObjectNative(long j, int i);

    private static native int executeNative(long j) throws IOException;

    private static native long getNative(String[] strArr);

    private static native long postNative(String[] strArr);

    private static native void setCipherNative(long j, String str);

    private static native void setConnectTimeoutNative(long j, int i);

    private static native void setGMVersionNative(long j, long j2);

    private static native void setReadTimeoutNative(long j, int i);

    private static native void setResultCallbackNative(long j, long j2, boolean z);

    public static MultiHttpRequest get(List<String> list) {
        if (list == null || list.isEmpty() || list.contains(null)) {
            throw new IllegalArgumentException("urls cannot be empty or null or contains null");
        }
        return new MultiHttpRequest(getNative((String[]) list.toArray(new String[list.size()])));
    }

    public static MultiHttpRequest post(List<String> list) {
        if (list == null || list.isEmpty() || list.contains(null)) {
            throw new IllegalArgumentException("urls cannot be null or empty or contains null");
        }
        return new MultiHttpRequest(postNative((String[]) list.toArray(new String[list.size()])));
    }

    public static MultiHttpRequest createRequests(List<String> list, List<String> list2) {
        if (list == null || list2 == null || list.isEmpty() || list2.isEmpty() || list.contains(null) || list2.contains(null)) {
            throw new IllegalArgumentException("urls and methods cannot be null or empty or contains null");
        }
        if (list.size() != list2.size()) {
            throw new IllegalArgumentException("urls.size() MUST be same as methods.size()");
        }
        return new MultiHttpRequest(createRequestsNative((String[]) list.toArray(new String[list.size()]), (String[]) list2.toArray(new String[list2.size()])));
    }

    public MultiHttpRequest setConnectTimeout(int i) {
        ensureNotClosed();
        setConnectTimeoutNative(this.mNativePtr, i);
        return this;
    }

    public MultiHttpRequest setReadTimeout(int i) {
        ensureNotClosed();
        setReadTimeoutNative(this.mNativePtr, i);
        return this;
    }

    public MultiHttpRequest setCipher(String str) {
        ensureNotClosed();
        if (str == null) {
            str = "";
        }
        setCipherNative(this.mNativePtr, str);
        return this;
    }

    public MultiHttpRequest setGMVersion(long j) {
        ensureNotClosed();
        setGMVersionNative(this.mNativePtr, j);
        return this;
    }

    private int dispatchResultCallback(String str, int i, long j) {
        if (this.mResultCallback == null) {
            Log.error(TAG, "mResultCallback not exist");
            return -1;
        }
        return this.mResultCallback.onResultCallback(str, i, j != 0 ? new HttpResponse(j) : null);
    }

    public MultiHttpRequest setResultCallback(ResultCallback resultCallback) {
        ensureNotClosed();
        this.mResultCallback = resultCallback;
        setResultCallbackNative(this.mNativePtr, this.mNativeWeakSelfPtr, resultCallback == null);
        return this;
    }

    public int execute() throws IOException {
        ensureNotClosed();
        return executeNative(this.mNativePtr);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.mClosed) {
            throw new IllegalStateException("invoke method on a closed object");
        }
        this.mClosed = true;
        this.mStackTrace = null;
        destroyObjectNative(this.mNativeWeakSelfPtr, 2);
        destroyObjectNative(this.mNativePtr, 1);
    }

    protected void finalize() throws Throwable {
        if (!this.mClosed) {
            Log.warn(TAG, "MultiHttpRequest instance not closed, create by ", this.mStackTrace);
            close();
        }
        super.finalize();
    }

    private void ensureNotClosed() {
        if (this.mClosed) {
            throw new IllegalStateException("invoke method on a closed object");
        }
    }

    private void ensureNotOnMainThread() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            throw new NetworkOnMainThreadException();
        }
    }

    private static <T> T checkNotNull(T t, String str) {
        if (t != null) {
            return t;
        }
        throw new NullPointerException(str);
    }

    private MultiHttpRequest(long j) {
        this.mNativePtr = j;
    }
}
